package com.cheer.ba.view.activity;

import android.content.Intent;
import android.os.CountDownTimer;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.cheer.ba.R;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.model.PushMessageModel;
import com.cheer.ba.utils.ClearCacheUtil;
import com.cheer.ba.utils.Protect;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.utils.SystemUtils;
import com.cheer.ba.view.base.BaseAty;

/**
 * 作者：Administrator on 2016/6/22 0022 11:07
 * 功能：欢迎页+广告页
 * 说明：展示欢迎页+广告页
 */
public class SplashAty extends BaseAty {

    /**
     * 跳过
     */
    private TextView mTxtjumpTo;

    /**
     * 启动页背景
     */
    private ImageView mImgBg;

    /**
     * 界面广告停留时间
     */
    private int recLen;

    /**
     * 倒计时类
     */
    private MyCount mc;

    //是否点击广告
    private boolean isClickAdv;

    //时间是否结束
    private boolean isTimeOver;

    //记录是否点击通知
    private int type;

    @Override
    public View loadView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.aty_splash, null);
    }

    @Override
    protected boolean isTitleBarShow() {
        return false;
    }

    @Override
    protected boolean isSupportSwipeback() {
        return false;
    }


    @Override
    public void initView() {
        mImgBg = (ImageView) findViewById(R.id.id_start_page_bg_img);
        mImgBg.setImageResource(R.drawable.start_page_bg);
        if (!TextUtils.equals(StringUtils.isEmpty(Session.getAppVersion()) ? "" : Session.getAppVersion(), SystemUtils.getVersionName(this))) {
            ClearCacheUtil.clearCache(aty, 0, "");
            Session.clearMust();
            Session.setAppVersion(SystemUtils.getVersionName(this));
        }

        type = getIntent().getIntExtra(IntentKey.General.KEY_TYPE, -1);

        // 判断用户是否是第一次安装
        if (!Session.getUserIsFirstDownload()) {
            initV();
        } else {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    skipActivity(aty, GuideAty.class);
                }
            }, 2000);
        }
    }

    @Override
    public void initData() {
        SystemUtils.checkSystem(this);
    }

    private void initV() {
        //当前日期
        long currentTime = System.currentTimeMillis();
        //上刊日期
        long upIngDate = StringUtils.isEmpty(Session.getAdv_upIngDate()) ? 0 : Long.parseLong(Session.getAdv_upIngDate());
        //过期日期
        long expireDate = StringUtils.isEmpty(Session.getAdv_expireDate()) ? 0 : Long.parseLong(Session.getAdv_expireDate());
        Log.d("time-----", "当前时间" + currentTime + "上刊时间" + upIngDate + "过期日期" + expireDate);
        if (currentTime >= upIngDate && currentTime < expireDate) {
            recLen = 1000 * 5;
            if (Session.getAdv_pic_url().endsWith(".gif")) {
                if (Protect.checkLoadImageStatus(aty)) {
                    Glide.with(aty).load(Session.getAdv_pic_url()).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(mImgBg);
                }
            } else {
                if (Protect.checkLoadImageStatus(aty)) {
                    Glide.with(aty).load(Session.getAdv_pic_url()).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.ALL).into(mImgBg);
                }
            }
            if (!StringUtils.isEmpty(Session.getAdv_url())) {
                mImgBg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        isClickAdv = true;
                        CommH5Aty.startCommonH5(aty, Session.getAdv_url(), false);
                    }
                });
            }
        } else {
            recLen = 1000 * 3;
            mImgBg.setImageResource(R.drawable.start_page_bg);
        }
        mTxtjumpTo = (TextView) findViewById(R.id.id_jump_txt);
        mTxtjumpTo.setVisibility(View.VISIBLE);
        mTxtjumpTo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isTimeOver = true;
                jumpTo();
            }
        });
        //界面广告停留时间
        mc = new MyCount(recLen, 1000);
        mc.start();
    }

    @Override
    protected void onResume() {
        super.onResume();
        isClickAdv = false;
        if (isTimeOver) {

            skipActivity(aty, MainAty.class);
        }
    }

    /**
     * 跳转
     */
    private void jumpTo() {
//        if (Session.getIsOpenGesture()) {
//            Intent intent = new Intent(aty, LockCheckActivity.class);
//            intent.putExtra("gesture", Constants.HOM_PAGE_TO_GESTURE);
//            skipActivity(this, intent);
//        } else {
//        }
        if (!isClickAdv) {
            Intent intent = new Intent(aty, MainAty.class);
            if (type == IntentCode.Jpush.JPUSH_CLICK) {
                PushMessageModel model = (PushMessageModel) getIntent().getSerializableExtra(IntentKey.General.KEY_MODEL);
                if (model != null) {
                    intent.putExtra(IntentKey.General.KEY_MODEL, model);
                }
                intent.putExtra(IntentKey.General.KEY_TYPE, IntentCode.Jpush.JPUSH_CLICK);
            }
            skipActivity(aty, intent);
        }
    }

    /**
     * 倒计时类
     */
    public class MyCount extends CountDownTimer {
        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long l) {
            mTxtjumpTo.setText(getString(R.string.start_page_jump) + "(" + l / 1000
                    + getString(R.string.second) + ")");
        }

        @Override
        public void onFinish() {
            isTimeOver = true;
            jumpTo();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mc) {
            mc.cancel();
        }
    }
}
